<?php

namespace App\Kuafu\User\Service;

use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Common\Enum\CodeEnum;
use App\Kuafu\User\Enum\RoleCodeEnum;
use App\Kuafu\User\Model\Role;
use Illuminate\Http\Request;

class RoleService extends BaseService
{

    public static function getRoleList($brandId, $shopId, $pageSize, $pageNum, object $params)
    {
        $result = Role::getRoleListWithType($brandId, $shopId, $pageSize, $pageNum, $params);

        return $result;
    }

    /**
     * 查询角色
     *
     * @param $request
     * @return array
     */
    public static function getRoleInfo($roleId)
    {
        $result = Role::query()->where('id', $roleId)
                      ->first(['id', 'name', 'brandId', 'shopId', 'status', 'createdAt', 'updatedAt', 'type']);

        $data = !empty($result) ? $result->toArray() : [];

        return $data;
    }

    /**
     * 删除角色
     *
     * @param Request $request
     * @return array
     */
    public static function deleteRole(int $roleId)
    {
        if (!Role::query()->where('id', $roleId)->update(['isDeleted' => 1])) {
            return self::returnData(BaseCodeEnum::ERR_DB_ERROR);
        }

        return self::returnData(BaseCodeEnum::OK);
    }

    public static function getUserAllRole()
    {

    }

    /**
     * 根据 platform 返回 type 类型
     *
     * @return int
     */
    public static function switchPlatformToType(int $platform): int
    {
        switch ($platform) {
            case CodeEnum::PLATFORM_POS;
                return RoleCodeEnum::ROLE_POS_TYPE;
            case CodeEnum::PLATFORM_B_BRAND:
            case CodeEnum::PLATFORM_B_SHOP:
                return RoleCodeEnum::ROLE_WEB_TYPE;
            case CodeEnum::PLATFORM_BUSINESS:
                return RoleCodeEnum::ROLE_BUSINESS_TYPE;
        }
        return 0;
    }

    /**
     * 根据 type 返回 platform 类型
     *
     * @return int
     */

    public static function switchTypeToPlatform(int $type): int
    {
        switch ($type) {
            case RoleCodeEnum::ROLE_POS_TYPE;
                return CodeEnum::PLATFORM_POS;
            case RoleCodeEnum::ROLE_WEB_TYPE:
                return CodeEnum::PLATFORM_B_BRAND;
            case RoleCodeEnum::ROLE_BUSINESS_TYPE:
                return CodeEnum::PLATFORM_BUSINESS;
        }
        return 0;
    }
}
